package com.nbjtjc.safe.mapper.sys;

import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.model.sys.SysUser;
import com.nbjtjc.util.MyMapper;
import org.activiti.engine.identity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SysUserMapper extends MyMapper<SysUser> {


    @Select("select sys_user.*,sys_dept.name as deptName from sys_user " +
            "LEFT JOIN sys_dept" +
            "    ON sys_user.dept_id=sys_dept.id" +
            " where login_name=#{loginName} and password=#{pwd}")
    @ResultMap("selectUserDeptNameMap")
    SysUser find(@Param("loginName") String loginName,@Param("pwd") String pwd);
    @Select("select * from sys_user where dept_id=#{deptId}")
    List<SysUser> selectByDeptId(@Param("deptId") Integer deptId);
    @Select("select * from sys_user where name=#{name}")
    SysUser selectByName(@Param("name") String name);

    @Select("select * from sys_user  " +
            "LEFT JOIN sys_dept" +
            "    ON sys_user.dept_id=sys_dept.id " +
            " where user_id=#{userId}")
    @ResultMap("selectUserDeptNameMap")
    SysUser selectByUserId(@Param("userId") Integer userId);
    List<SysUser> search(@Param("user") SysUser sysUser,@Param("page") Page page);
    
    @Select("select a.*,b.name as deptName,c.parent_id as  parentCompanyId from sys_user  a LEFT JOIN sys_dept b ON a.dept_id=b.id left join sys_dept c on a.company_id=c.id where a.user_id=#{userId}")
    SysUser findByUserId(@Param("userId") Integer userId);

    @Select("select username from sys_user where user_id=#{userId}")
     String findNameByUserId(@Param("userId") int userId);
    
//    @Select("SELECT DISTINCT DATE_FORMAT(entry_date,'%Y') AS 'year' FROM sys_user WHERE  entry_date IS NOT NULL")
//    List<String> findYear();
    @Select("SELECT DISTINCT DATE_FORMAT(b.entry_date,'%Y') AS 'year' FROM edu_entry_training a INNER JOIN sys_user b ON a.user_id = b.user_id WHERE a.company_id = #{companyId} and b.entry_date IS NOT NULL")
    List<String> findYear(@Param("companyId")Integer companyId);


    @Select("<script>"+
    " select user_id from sys_user where company_id in " +
            " <foreach item='companyId' index='index' collection='companyIds'      open='(' separator=',' close=')'> " +
            "#{companyId}" +
            " </foreach> " +
            "</script>")
    List<Integer> findUserIdsByCompanyIds(@Param("companyIds") List<Integer> companyIds);

    @Select("select * from sys_user where open_id=#{openId}")
    SysUser findByOpenId(@Param("openId") String openId);

    @Update("update sys_user set head_file_id=#{fileId} where user_id=#{userId}")
    int setUserHead(@Param("userId") int userId,@Param("fileId") int fileId);
}